#import important libraries
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
import plotly.io as pio
pio.templates.default = "plotly_white"
#read the downloaded csv file
data = pd.read_csv('Downloads/rides.csv')
print(data.head())
Drivers Active Per Hour Riders Active Per Hour Rides Completed 0 72 295 202.0 1 50 78 43.0 2 40 250 181.0 3 78 140 124.0 4 74 195 108.0
#see if the dataset has null values
print(data.isnull().sum())
Drivers Active Per Hour 0 Riders Active Per Hour 0 Rides Completed 54 dtype: int64
#drop all the null values and recheck if all null values are removed
data = data.dropna()
print(data.isnull().sum())
Drivers Active Per Hour 0 Riders Active Per Hour 0 Rides Completed 0 dtype: int64
#Analyze the relationship between the number of drivers active per hour and
#the number of riders active per hour
demand = data["Riders Active Per Hour"]
supply = data["Drivers Active Per Hour"]
figure = px.scatter(data, x = "Drivers Active Per Hour",
y = "Riders Active Per Hour",
trendline = "ols",
title = "Demand and Supply Analysis")
figure.update_layout(
xaxis_title = "Numbers of Drivers Active Per Hour (Supply)",
yaxis_title = "Numbers of Riders Active Per Hour (Demand)")
figure.show()
#From the above fig we can see a constant relationship between the number of drivers per hour
#and the number of riders active per hour
#Now let's calculate the elasticity of demand for rides concerning the number of active drivers
#per hour
#Calculate elasticity
avg_demand = data["Riders Active Per Hour"].mean()
avg_supply = data["Drivers Active Per Hour"].mean()
pct_change_demand = (max(data["Riders Active Per Hour"]) - min(data["Riders Active Per Hour"])) / avg_demand * 100
pct_change_supply = (max(data["Drivers Active Per Hour"]) - min(data["Drivers Active Per Hour"])) / avg_supply * 100
elasticity = pct_change_demand / pct_change_supply
print("Elasticity of demand with respect to the number of active drivers per hour : {:.2f}".format(elasticity))
Elasticity of demand with respect to the number of active drivers per hour : 0.82
#Now let's add a new column in the dataset by calculating the supply ratio
#Calculate the supply ratio for each level of driver activity
data["Supply Ratio"] = data["Rides Completed"] / data["Drivers Active Per Hour"]
print(data.head())
Drivers Active Per Hour Riders Active Per Hour Rides Completed \ 0 72 295 202.0 1 50 78 43.0 2 40 250 181.0 3 78 140 124.0 4 74 195 108.0 Supply Ratio 0 2.805556 1 0.860000 2 4.525000 3 1.589744 4 1.459459
#Now let's visualize the supply ratio
fig = go.Figure()
fig.add_trace(go.Scatter(x=data["Drivers Active Per Hour"],
y=data["Supply Ratio"], mode ="markers"))
fig.update_layout(
title = "Supply Ratio vs Driver Activity",
xaxis_title = "Driver Activity(Drivers Active Per Hour)",
yaxis_title = "Supply Ratio (Rides Completed per Driver Active per Hour)"
)
fig.show()
#The above graph shows the ratio of the number of drivers active per hour and the number of rides
#completed in an hour. So this is how we can analyze demand and supply using the Python programming
#language.
#Summary
#Demand and Supply analysis means analyzing the relationship between the quantity demanded and
#the quantity supplied. It helps businesses understand the factors influencing consumer demand
#to maximize profits.